import createAxiosClient from "@/app/utils/axiosRequest";
import ToastUtil from "@/app/utils/toastUtil";
import cookie from "react-cookies";
import { Token } from "@/app/constants/authTokenConstants";

// 创建笔记参数接口
export interface CreateNoteParams {
  /* AI模式 */
  mode: string;

  /* 用户提示词 */
  prompt: string;

  /* 语言 */
  language?: string;

  /* 额外参数 */
  extraParams?: string;
}

// AI响应接口
export interface AIResponseDTO {
  /* 会话ID */
  conversationId?: number;
  
  /* AI模式 */
  mode?: string;
  
  /* 生成的标题 */
  title?: string;
  
  /* 生成的内容 */
  content?: string;
  
  /* 结构化数据 */
  structuredData?: string;
  
  /* 元数据 */
  metadata?: string;
  
  /* 建议的标签 */
  suggestedTags?: string[];
  
  /* 创建时间 */
  createdAt?: string;
}

/** 
 * AI辅助创建笔记
 * @param {object} params AI辅助创建笔记请求
 * @param {string} params.mode AI模式
 * @param {string} params.prompt 用户提示词
 * @param {string} params.language 语言
 * @param {string} params.extraParams 额外参数
 * @returns Promise<AIResponseDTO>
 */
export async function createNote(params: CreateNoteParams): Promise<AIResponseDTO> {
  // 使用统一的axios实例
  const apiClient = createAxiosClient();
  
  try {
    const response = await apiClient.post(`/app/ai/create`, params);
    
    if (response.data.code === 200) {
      return response.data.data;
    } else {
      ToastUtil(response.data.message || "AI创建笔记失败", "❌", "error");
      throw new Error(response.data.message || "AI创建笔记失败");
    }
  } catch (error: any) {
    ToastUtil(error.message || "AI创建笔记异常", "❌", "error");
    throw error;
  }
}

// 编辑笔记参数接口
export interface EditNoteParams {
  /* 笔记ID */
  noteId: number;

  /* AI模式 */
  mode: string;

  /* 用户提示词 */
  prompt?: string;

  /* 语言 */
  language?: string;

  /* 额外参数 */
  extraParams?: string;

  /* 是否包含笔记内容 */
  includeContent?: boolean;

  /* 笔记内容 */
  content?: string;

  /* 笔记标题 */
  title?: string;
}

/** 
 * AI辅助编辑笔记
 * @param {object} params AI辅助编辑笔记请求
 * @param {number} params.noteId 笔记ID
 * @param {string} params.mode AI模式
 * @param {string} params.prompt 用户提示词
 * @param {string} params.language 语言
 * @param {string} params.extraParams 额外参数
 * @param {boolean} params.includeContent 是否包含笔记内容
 * @param {string} params.content 笔记内容
 * @param {string} params.title 笔记标题
 * @returns Promise<AIResponseDTO>
 */
export async function editNote(params: EditNoteParams): Promise<AIResponseDTO> {
  // 使用统一的axios实例
  const apiClient = createAxiosClient();
  
  try {
    const response = await apiClient.post(`/app/ai/edit`, params);
    
    if (response.data.code === 200) {
      return response.data.data;
    } else {
      ToastUtil(response.data.message || "AI编辑笔记失败", "❌", "error");
      throw new Error(response.data.message || "AI编辑笔记失败");
    }
  } catch (error: any) {
    ToastUtil(error.message || "AI编辑笔记异常", "❌", "error");
    throw error;
  }
}

// AI对话历史接口
export interface AIConversation {
  id: number;
  userId: number;
  noteId?: number;
  mode: string;
  prompt: string;
  extraParams?: string;
  response: string;
  responseSummary?: string;
  metadata?: string;
  createdAt: string;
  updatedAt?: string;
  deleted?: number;
}

/**
 * 获取AI对话历史
 * @param page 页码，从0开始
 * @param size 每页大小
 * @returns 分页结果
 */
export async function getAIHistory(page: number = 0, size: number = 10): Promise<any> {
  // 使用统一的axios实例
  const apiClient = createAxiosClient();
  
  try {
    const response = await apiClient.get(`/app/ai/history`, {
      params: {
        page,
        size
      }
    });
    
    if (response.data.code === 200) {
      return response.data.data;
    } else {
      ToastUtil(response.data.message || "获取AI对话历史失败", "❌", "error");
      throw new Error(response.data.message || "获取AI对话历史失败");
    }
  } catch (error: any) {
    ToastUtil(error.message || "获取AI对话历史异常", "❌", "error");
    throw error;
  }
}

/**
 * 获取AI对话详情
 * @param conversationId 对话ID
 * @returns 对话详情
 */
export async function getAIConversation(conversationId: number): Promise<AIConversation> {
  // 使用统一的axios实例
  const apiClient = createAxiosClient();
  
  try {
    const response = await apiClient.get(`/app/ai/conversation/${conversationId}`);
    
    if (response.data.code === 200) {
      return response.data.data;
    } else {
      ToastUtil(response.data.message || "获取AI对话详情失败", "❌", "error");
      throw new Error(response.data.message || "获取AI对话详情失败");
    }
  } catch (error: any) {
    ToastUtil(error.message || "获取AI对话详情异常", "❌", "error");
    throw error;
  }
}

/**
 * 删除AI对话历史
 * @param conversationId 对话ID
 * @returns 是否成功
 */
export async function deleteAIConversation(conversationId: number): Promise<boolean> {
  // 使用统一的axios实例
  const apiClient = createAxiosClient();
  
  try {
    const response = await apiClient.delete(`/app/ai/conversation/${conversationId}`);
    
    if (response.data.code === 200) {
      return true;
    } else {
      ToastUtil(response.data.message || "删除AI对话历史失败", "❌", "error");
      return false;
    }
  } catch (error: any) {
    ToastUtil(error.message || "删除AI对话历史异常", "❌", "error");
    return false;
  }
}

// 聊天消息类型
export interface ChatMessage {
    role: string;
    content: string;
}

/**
 * 与AI聊天
 * @param messages 聊天消息数组
 * @returns Promise<AIResponseDTO>
 */
export async function chatWithAI(messages: ChatMessage[]): Promise<AIResponseDTO> {
  // 使用统一的axios实例
  const apiClient = createAxiosClient();
  
  try {
    const params = {
      mode: "CHAT",
      messages: messages,
      language: "zh"
    };
    
    const response = await apiClient.post(`/app/ai/chat`, params);
    
    if (response.data.code === 200) {
      return response.data.data;
    } else {
      ToastUtil(response.data.message || "AI聊天失败", "❌", "error");
      throw new Error(response.data.message || "AI聊天失败");
    }
  } catch (error: any) {
    ToastUtil(error.message || "AI聊天异常", "❌", "error");
    throw error;
  }
} 